package dataset.generation;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.util.InvalidFormatException;

public class SentenceCreatorDeprecated {

	private static int ContentStart;

	private static SentenceDetectorME sentenceDetector;

	private static FileInputStream modelIn;

	private static SentenceModel model;
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InvalidFormatException 
	 */
	public static void main(String[] args) throws InvalidFormatException, IOException {
		
		modelIn = new FileInputStream("en-sent.bin");
		
		model = new SentenceModel(modelIn);

		sentenceDetector = new SentenceDetectorME(model);
		
		String fileName = "/home/pjbarrio/Documents/RelationExtractionStuff/JSRE/jsre/jsre-1.1/jsre-1.1/examples/natural_disaster/files/test";
		
		String directoryName = "/home/pjbarrio/Documents/RelationExtractionStuff/JSRE/jsre/jsre-1.1/jsre-1.1/examples/natural_disaster/files/";
		
		String[] Tags = {"NATURALDISASTER","LOCATION"};
		
		String[] RelationTags = {"atLocation","locationOf"};
		
		File directory = new File(directoryName);
		
		File[] files = directory.listFiles();
		
		System.setOut(new PrintStream(new File(fileName)));
		
		for (File file : files) {
			
			System.out.println(file);
			
			List<String> sentences = getRelevantSentences(file,Tags,RelationTags); 
			
			for (String sentence : sentences) {
				
				System.out.println(sentence);
				
			}
			
		}

	}

	private static List<String> getRelevantSentences(File file, String[] tags, String[] relationTags) throws IOException {
		
		String text = readFile(file);
		
		String plainText = generatePlainText(text,tags,relationTags);
		
		String[] segmented = segmentText(plainText);
		
		String[] sentences = generateSentences(segmented);

		for (String sentence : sentences) {
			
			System.err.println("SENTENCE: " + sentence);
			
		}
		
		Hashtable<String, ArrayList<String>> tagsStart = detectTags(text,tags);
		
		Hashtable<String,ArrayList<String>> relationTagsStart = detectRelationTags(text,relationTags);
		
		return generateSentencesWithLabels(text,relationTagsStart,tagsStart,tags,relationTags);
		
	}

	private static String[] generateSentences(String[] contentParagraph) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		for (int i = 0; i < contentParagraph.length; i++) {
			
			ret.addAll(getSentences(contentParagraph[i]));
			
		}

		return ret.toArray(new String[0]);
		
	}

	private static Collection<? extends String> getSentences(String text) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		String sentences[] = sentenceDetector.sentDetect(text);
			
		for (int i = 0; i < sentences.length; i++) {

			ret.add(sentences[i]);

		}

		return ret;
		
	}

	
	private static String[] segmentText(String text) throws IOException {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		BufferedReader br = new BufferedReader(new StringReader(text));
	    
	    String line = br.readLine();
	    
	    String acum = "";
	    
	    while (line != null){
	    	
	    	if (line.trim().equals("")){
	    			    		
	    		if (!acum.trim().equals(""))
	    			ret.add(acum);
	    		
	    		acum = "";
	    	
	    	} else if (acum.trim().endsWith(".")){
	    	
	    		ret.add(acum);
	    		
	    		acum = line;
	    	
	    	}
	    	else {
	    		
	    		acum = acum + " " + line;
	    		
	    	}
	    	
	    	line = br.readLine();
	    }
	    
	    br.close();
	    
	    return ret.toArray(new String[0]);
		
	}

	private static String generatePlainText(String text, String[] tags,
			String[] relationTags) {
		
		String noTagsString = text;
		
		for (int i = 0; i < tags.length; i++) {
			
			noTagsString = noTagsString.replaceAll("\\</"+ tags[i] +".*?>","").replaceAll("\\<"+ tags[i] +".*?>", "");
			
		}
		
		for (int i = 0; i < relationTags.length; i++) {
			
			noTagsString = noTagsString.replaceAll("\\</"+ relationTags[i] +".*?>","").replaceAll("\\<"+ relationTags[i] +".*?>", "");
			
		}

		return noTagsString;
		
	}

	private static Hashtable<String, ArrayList<String>> detectTags(String text,
			String[] tags) {

		Hashtable<String, ArrayList<String>> rethash = new Hashtable<String, ArrayList<String>>();
		
		for (int i = 0; i < tags.length; i++) {

			ArrayList<String> ret = new ArrayList<String>();
			
			// Compile and use regular expression
			Pattern pattern = Pattern.compile("<" + tags[i] + ">(.*?)</" + tags[i] +">");
			Matcher matcher = pattern.matcher(text);
			
			while  (matcher.find()) {
			    
				ret.add(matcher.start(0) + "," + matcher.group(0));

			}
			
			rethash.put(tags[i],ret);
			
		}
		
		return rethash;

		
	}

	private static List<String> generateSentencesWithLabels(String text,
			Hashtable<String, ArrayList<String>> relationTagsStart, Hashtable<String,ArrayList<String>> tagsStart, String[] tags, String[] relationTags) {
		
		List<List<String>> order1 = findRelations(tags[0],tags[1],tagsStart);
		
		List<List<String>> order2 = findRelations(tags[1],tags[0],tagsStart);
		
		
		
		
		return new ArrayList<String>();
		
	}


	private static List<List<String>> findRelations(String first,
			String second, Hashtable<String, ArrayList<String>> tagsStart) {
		
		List<List<String>> ret = new ArrayList<List<String>>();
		
		List<String> firstElement = tagsStart.get(first);
		
		List<String> secondElement = tagsStart.get(second);
		
		for (String firstE : firstElement) {

			for (String secondE : secondElement) {
				
				if (isAcceptable(firstE,secondE)){
					
					ret.add(generateList(firstE,secondE));
					
				}
				
			}
			
		}

		return ret;
	}

	private static List<String> generateList(String firstE, String secondE) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		ret.add(firstE);
		
		ret.add(secondE);
		
		return ret;
		
	}

	private static boolean isAcceptable(String first, String second) {

		
		
		int _1 = Integer.valueOf(first.substring(0, first.indexOf(',')));
		
		int _2 = Integer.valueOf(second.substring(0, second.indexOf(',')));
		
		System.err.println(_1 + "  --   " + _2);
		
		return _1 < _2;
		
	}

	private static Hashtable<String, ArrayList<String>> detectRelationTags(String text,
			String[] relationTags) {
		
		Hashtable<String, ArrayList<String>> rethash = new Hashtable<String, ArrayList<String>>();
		
		for (int i = 0; i < relationTags.length; i++) {

			ArrayList<String> ret = new ArrayList<String>();
			
			// Compile and use regular expression
			Pattern pattern = Pattern.compile("<" + relationTags[i] + ".*?>(.*?)</" + relationTags[i] +">");
			Matcher matcher = pattern.matcher(text);
			
			while  (matcher.find()) {
			    // Get all groups for this match
//			    for (int j=0; j<=matcher.groupCount(); j++) {
//			        String groupStr = matcher.group(j);
			    
				ret.addAll(detectedRecursive(matcher.group(0),relationTags[i],matcher.start(0)));
				
		
//			    }
			}
			
			rethash.put(relationTags[i],ret);
			
		}
		
		return rethash;
		
	}

	private static Collection<? extends String> detectedRecursive(String extraction, String relationTag, int start) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		ArrayList<String> openTags = detectOpenTags(extraction,relationTag);
		
		String content = detectContent(extraction,openTags.get(openTags.size()-1),relationTag,start);
		
		for (String openTag : openTags) {
			//save the start point of the content.
			ret.add(ContentStart + "," + openTag + content + "</" + relationTag + ">");
			
		}
		
		return ret;
		
	}

	private static String detectContent(String extraction, String lastOpenTag, String relationTag, int start) {

		Pattern pattern = Pattern.compile(lastOpenTag + "(.*?)</" + relationTag +">");
		
		Matcher matcher = pattern.matcher(extraction);
		
		
		
		if  (matcher.find()) {
		
			ContentStart = start + matcher.start(1);
			
			return matcher.group(1);
			
		}
		
		return "NO_CONTENT";
		
	}

	private static ArrayList<String> detectOpenTags(String extraction,
			String relationTag) {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		Pattern pattern = Pattern.compile("<" + relationTag + ".*?>");
		Matcher matcher = pattern.matcher(extraction);
		
		while  (matcher.find()) {
		    for (int j=0; j<=matcher.groupCount(); j++) {
     
		        ret.add(matcher.group(0));
			
		    }
		}
		
		return ret;
		
	}

	private static String readFile(File file) {
		
		byte[] buffer = new byte[(int) file.length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
	        f.read(buffer);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    
	    return new String(buffer);
		
	}

}
